﻿using System.Linq;
using System.Windows;
using Microsoft.Win32;
using RK.Common.GraphicsEngine.Objects;
using RK.Common.GraphicsEngine.Objects.Wpf;
using RK.WpfModelViewer.Modules.ACImporter;

namespace RK.WpfModelViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string m_currentModelFile;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow" /> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            UpdateDialogState();
        }

        /// <summary>
        /// Loads the model from the given path.
        /// </summary>
        /// <param name="modelFile">The model file to load.</param>
        public void LoadModel(string modelFile)
        {
            m_viewport3D.Visual3DChildren.Clear();

            ObjectType appleType = ObjectType.FromACFile(modelFile);
            LoadModel(appleType);

            m_currentModelFile = modelFile;

            UpdateDialogState();
        }

        private void LoadModel(ObjectType appleType)
        {
            WpfGenericModel genericModel = new WpfGenericModel() { ObjectType = appleType };
            genericModel.FitToCenteredCuboid = false;

            //genericModel.FitToCenteredCuboidMode = FitToCuboidMode.MaintainAspectRatio;
            //genericModel.FitToCenteredCuboidOrigin = FitToCuboidOrigin.LowerCenter;
            //genericModel.FitToCenteredCuboidWidth = genericModel.FitToCenteredCuboidHeight = genericModel.FitToCenteredCuboidDepth = 3f;
            m_viewport3D.Visual3DChildren.Add(genericModel);
        }

        /// <summary>
        /// Clears current 3d scene.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        private void OnCmdCloseClick(object sender, RoutedEventArgs e)
        {
            m_viewport3D.Visual3DChildren.Clear();

            m_currentModelFile = string.Empty;
            UpdateDialogState();
        }

        /// <summary>
        /// Called when user wants to load a model.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        private void OnCmdLoadModelClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "AC-Files (*.ac)|*.ac";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog(this) == true)
            {
                LoadModel(openFileDialog.FileName);
            }
        }

        private void OnCmdSaveAsXamlClick(object sender, RoutedEventArgs e)
        {
            WpfConstructed3DModel model = m_viewport3D.Visual3DChildren.FirstOrDefault() as WpfConstructed3DModel;
            if (model == null) { return; }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Xaml-Files (*.xaml)|*.xaml";
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog(this) == true)
            {
                model.SaveModelAsXamlTo(saveFileDialog.FileName);
            }
        }

        private void UpdateDialogState()
        {
            string title = "RK Model Viewer";
            if (!string.IsNullOrEmpty(m_currentModelFile)) { title += " - " + m_currentModelFile; }
            this.Title = title;
        }

        private void OnCmdLoadModelFromX(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "X (*.x)|*.x";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog(this) == true)
            {
                XModelImporter xImporter = new XModelImporter();
                LoadModel(xImporter.ImportObjectType(openFileDialog.FileName));

                m_currentModelFile = openFileDialog.FileName;
                UpdateDialogState();
            }
        }
    }
}